﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace Asol.Reporting.Editor.Components
{
    public class OptionButtonPanel : Panel
    {
        public OptionButtonPanel()
        {
            this.InitializeComponent();
        }
        private void InitializeComponent()
        {
            this._OptionButton1 = this.CreateButton("Alphabetical", "A", new Rectangle(0, 0, 100, 25));
            this._OptionButton2 = this.CreateButton("Categorized", "C", new Rectangle(100, 0, 100, 25));
            this.Size = new System.Drawing.Size(200, 25);
            this.Value = "A";
            this.SizeChanged += new EventHandler(OptionButtonPanel_SizeChanged);
            this.SetLayout();
        }
        void OptionButtonPanel_SizeChanged(object sender, EventArgs e)
        {
            this.SetLayout();
        }
        private void SetLayout()
        {
            int b = 1;
            int w = (this.Width - 3 * b) / 2;
            int h = this.Height - 2 * b;
            this._OptionButton1.Bounds = new Rectangle(b, b, w, h);
            this._OptionButton2.Bounds = new Rectangle(2 * b + w, b, w, h);
        }

        private OptionButton CreateButton(string text, IComparable value, Rectangle bounds)
        {
            OptionButton button = new OptionButton();
            button.Appearance = System.Windows.Forms.Appearance.Button;
            button.AutoSize = false;
            button.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
            button.FlatAppearance.BorderSize = 0;
            button.FlatAppearance.CheckedBackColor = SystemColors.Info;
            button.Padding = new System.Windows.Forms.Padding(0);
            button.Margin = new System.Windows.Forms.Padding(0);
            button.TextAlign = ContentAlignment.MiddleLeft;
            button.UseCompatibleTextRendering = true;
            button.Bounds = bounds;
            button.Text = text;
            button.Value = value;
            button.UseVisualStyleBackColor = true;
            button.CheckedChanged += new EventHandler(_Button_CheckedChanged);
            this.Controls.Add(button);
            return button;
        }

        void _Button_CheckedChanged(object sender, EventArgs e)
        {
            this.OnValueChanged();
        }
        private void OnValueChanged(bool force)
        {
            if (force)
                this._SuppressValueChanged = false;
            this.OnValueChanged();
        }
        private void OnValueChanged()
        {
            if (!this._SuppressValueChanged && this.ValueChanged != null)
                this.ValueChanged(this, EventArgs.Empty);
        }
        private bool _SuppressValueChanged;
        private OptionButton _OptionButton1;
        private OptionButton _OptionButton2;

        public IComparable Value
        {
            get
            {
                foreach (Control control in this.Controls)
                {
                    OptionButton button = control as OptionButton;
                    if (button != null && button.Checked)
                        return button.Value;
                }
                return null;
            }
            set
            {
                this._SuppressValueChanged = true;        // Event o změně zavolám až na konci, najednou.
                bool set = true;
                foreach (Control control in this.Controls)
                {
                    OptionButton button = control as OptionButton;
                    if (button != null)
                    {
                        bool isEqual = (button.Value.CompareTo(value) == 0);
                        button.Checked = isEqual && set;
                        if (isEqual)
                            set = false;
                    }
                }
                this.OnValueChanged(true);
                this.Refresh();
            }
        }
        public event EventHandler ValueChanged;
    }
    public class OptionButton : RadioButton
    {
        public IComparable Value { get; set; }
    }
}
